package cn.tedu.charging.order.quartz;


import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class DeviceCheckJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("此位置要检查设备状态。。。");
        //1.获取成功定的和失败订单的mapper对象
        OrderSuccessMapper orderSuccessMapper =
                ApplicationContextProvider.getContext()
                .getBean(OrderSuccessMapper.class);
        OrderFailMapper orderFailMapper =
                ApplicationContextProvider.getContext()
                        .getBean(OrderFailMapper.class);
        //2.基于订单id获取成功订单信息，假如存在表示订单正常，我们可以什么也不做
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        String orderNo=(String)jobDataMap.get("orderNo");
        ChargingBillSuccessPO chargingBillSuccessPO =
                orderSuccessMapper.selectById(orderNo);
        if(chargingBillSuccessPO!=null){
            System.out.println("订单正常,正在充电");
            //可以更新一下订单状态，显示订单充电中.
            //orderSuccessMapper.update()
            return;
        }
        //3.基于订单id获取失败订单信息，假如存在表示订单已失败，我们可以什么也不做
        ChargingBillFailPO chargingBillFailPO =
                orderFailMapper.selectById(orderNo);
        if(chargingBillFailPO!=null){
            System.out.println("设备自检失败了");
            return;
        }
        //4.没有成功订单，也没有失败订单，表示设备自检失败，并且没有返回给客户端，此时需要记录失败订单
        System.out.println("设备自检失败，插入失败订单记录");
        ChargingBillFailPO entity=new ChargingBillFailPO();
        entity.setBillId(orderNo);
        entity.setFailDesc("设备自检失败,并且无响应");
        orderFailMapper.insert(entity);//mybatis-plus
    }
}
